/*count num of bit 1*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* get_complement(signed int n)
{
    int bit_num=sizeof(n)*8;
    char *bits = malloc((bit_num+1)*sizeof(char));
    int flag = 1;
    int i = bit_num-1;

    while(flag)
    {
        *(bits+i) = n&flag?'1':'0';
        flag = flag<<1;
        i--;
    }
    bits[bit_num]= '\0';
    return bits;
}

int main(int argc, char **argv)
{
    char *s;
    int n = 3;
    if(argc!=2)
    {
        printf("you must provide 1(only) parameter.\n");
        srand(100);
        n = rand();
    }

    s=get_complement(n);
    printf("bits of %d:\n%s\n",n,s);
}
